#include "mysql.h"
int ls_l(MYSQL* conn,char* username,char* dirname){
    char query1[SQL_MAX]={0};
    sprintf(query1,"select parent_id from virtualfile where filename='%s';",dirname);
    mysql_query(conn,query1);
    MYSQL_RES * result1=mysql_store_result(conn);
    MYSQL_ROW parents=mysql_fetch_row(result1);
    int parentid=atoi(parents[0]);

    char query[SQL_MAX]={0};
    sprintf(query,"select T.username,T.filename,T.type,T.filesize from\
		(select user.username,vf.filename,vf.type,vf.filesize,vf.parent_id from user\
		inner join\
		virtualfile as vf\
		on user.username='%s') as T\
		where T.parent_id='%d';",username,parentid);
    mysql_query(conn,query);
    MYSQL_RES * result=mysql_store_result(conn);
    //int rows=mysql_num_rows(result);
    int cols=mysql_num_fields(result);

    MYSQL_ROW row;
    while((row=mysql_fetch_row(result))!=NULL){
        for(int i=0;i<cols;i++){
            printf(" %5s ",row[i]);
        }
        printf("\n");
    }
    mysql_free_result(result);
    mysql_close(conn);
}
int main(){
    MYSQL *conn=NULL;
    char* host="localhost";
    char* user="root";
    char* passwd="123456";
    char* db="yourdb";

    conn=mysql_init(NULL);
    mysql_real_connect(conn,host,user,passwd,db,0,NULL,0);
    ls_l(conn,"ppc","dir1");
    return 0;
}
